//
//  LoginView.m
//  Weiweiyunshi
//
//  Created by 王译 on 17/2/13.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "LoginView.h"
@interface LoginView ()<UITextFieldDelegate>

@end
@implementation LoginView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
        _i = 0 ;
    }
    return self;
}


-(void)createView{
    _backImageView = [[UIImageView alloc]init];
    //_backImageView.backgroundColor = RAN_COLOR
    _backImageView.image = [UIImage imageNamed:@"login_bg.jpg"];
    
    _scannerImageView = [[UIImageView alloc]init];
    _scannerImageView.image = [UIImage imageNamed:@"logo_font"];
    
    _userNameTextField = [[UITextField alloc]init];
    //_userNameTextField.backgroundColor = RAN_COLOR
    _userNameTextField.placeholder = @"登录账号";
    [_userNameTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _userNameTextField.delegate = self;
    
    _passWordTextField = [[UITextField alloc]init];
    _passWordTextField.placeholder = @"登录密码";
    [_passWordTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
   // _passWordTextField.backgroundColor = RAN_COLOR
    
    _passWordTextField.delegate = self;
    
    _loginBtn = [[UIButton alloc]init];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    _loginBtn.backgroundColor = RAN_COLOR
    
    _autoLoginLabel = [[UILabel alloc]init];
    _autoLoginLabel.text = @"自动登录";
   // _autoLoginLabel.backgroundColor = RAN_COLOR
    
    _autoLoginBtn = [[UIButton alloc]init];
    //自动登录图片添加
    [_autoLoginBtn setImage:[UIImage imageNamed:@"圆圈"] forState:UIControlStateNormal];
    [_autoLoginBtn setImage:[UIImage imageNamed:@"ok"] forState:UIControlStateSelected];
    
    _forgetBtn = [[UIButton alloc]init];
    _forgetBtn.backgroundColor = RAN_COLOR
    [_forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    /*
    _registeredBtn = [[UIButton alloc]init];
    [_registeredBtn setTitle:@"还没有账号?立即注册" forState:UIControlStateNormal];
    _registeredBtn.backgroundColor = RAN_COLOR
    _joinBtn = [[UIButton alloc]init];
    [_joinBtn setTitle:@"加入会议" forState:UIControlStateNormal];
    _joinBtn.backgroundColor = RAN_COLOR
     */
    [self sd_addSubviews:@[_backImageView,_scannerImageView,_userNameTextField,_passWordTextField,
                           _loginBtn,_autoLoginLabel,_autoLoginBtn,_forgetBtn,
                           ]];
    _backImageView.sd_layout
    .widthRatioToView(self,1)
    .heightRatioToView(self,1);
    
    _scannerImageView.sd_layout
    .widthRatioToView(self,0.7)
    .heightIs(100)
    .centerXEqualToView(self)
    .topSpaceToView(self,80);
    
    _userNameTextField.sd_layout
    .centerXEqualToView(self)
    .topSpaceToView(self,200)
    .widthRatioToView(self,0.7)
    .heightIs(40);
    
    _userNameTextField.backgroundColor = [UIColor colorWithRed:25 green:70 blue:97];
    _userNameTextField.layer.masksToBounds = YES;
    _userNameTextField.layer.cornerRadius = 3;
    _userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    UIView *leftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    

    UIImageView *view1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    view1.image = [UIImage imageNamed:@"userName"];
    [leftView addSubview:view1];
    
    _userNameTextField.leftView = leftView;
    
    UIImageView *view2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    view2.image = [UIImage imageNamed:@"pwd"];
    
    [leftView1 addSubview:view2];
    
    UIButton *lookBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    [lookBtn setImage:[UIImage imageNamed:@"look"] forState:UIControlStateNormal];
    
    
    [lookBtn setImage:[UIImage imageNamed:@"look1"] forState:UIControlStateSelected];

    [lookBtn addTarget:self action:@selector(lookpwd:) forControlEvents:UIControlEventTouchUpInside];
    
    _userNameTextField.textColor = [UIColor whiteColor];
    _passWordTextField.textColor = [UIColor whiteColor];
    
    _passWordTextField.sd_layout
    .leftEqualToView(_userNameTextField)
    .widthRatioToView(_userNameTextField,1)
    .heightRatioToView(_userNameTextField,1)
    .topSpaceToView(_userNameTextField,20);
    UIView *rightVeiw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [rightVeiw addSubview:lookBtn];
    _passWordTextField.leftViewMode = UITextFieldViewModeAlways;
    _passWordTextField.rightViewMode = UITextFieldViewModeAlways;
    _passWordTextField.leftView = leftView1;
    _passWordTextField.rightView = rightVeiw;
    
    _passWordTextField.layer.masksToBounds = YES;
    _passWordTextField.layer.cornerRadius = 3;
    _passWordTextField.secureTextEntry = YES;

    _passWordTextField.backgroundColor = [UIColor colorWithRed:25 green:70 blue:97];

    
    _loginBtn.sd_layout
    .centerXEqualToView(_userNameTextField)
    .widthRatioToView(_userNameTextField,1)
    .heightRatioToView(_userNameTextField,0.9)
    .topSpaceToView(_passWordTextField,20);
    
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.layer.cornerRadius = 3;
    _loginBtn.backgroundColor = [UIColor colorWithRed:83 green:215 blue:105];
    
    _autoLoginBtn.sd_layout
    .leftEqualToView(_userNameTextField)
    .widthIs(25)
    .heightIs(25)
    .topSpaceToView(_loginBtn,20);
    
    _autoLoginLabel.sd_layout
    .leftSpaceToView(_autoLoginBtn,5)
    .widthIs(100)
    .heightIs(30)
    .topSpaceToView(_loginBtn,20);
    
    _autoLoginLabel.textColor = [UIColor whiteColor];
    /*
    _forgetBtn.sd_layout
    .rightEqualToView(_loginBtn)
    .widthIs(100)
    .heightRatioToView(_autoLoginBtn,1)
    .topEqualToView(_autoLoginBtn);
     
    _registeredBtn.sd_layout
    .centerXEqualToView(self)
    .widthRatioToView(self,0.5)
    .heightIs(50)
    .topSpaceToView(_forgetBtn,50);
    
    _joinBtn.sd_layout
    .topSpaceToView(self,54)
    .rightSpaceToView(self,10)
    .widthRatioToView(self,0.2)
    .heightIs(30);
     */
}

-(void)lookpwd:(UIButton *)button{
    if (button.selected == NO) {
        _passWordTextField.secureTextEntry = NO;
        button.selected =YES;
        
    }else{
        _passWordTextField.secureTextEntry = YES;
        button.selected = NO;
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for(UIView *view in self.subviews)
    {
        [view resignFirstResponder];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}
@end
